<?php

namespace app\common\model;

use app\common\model\User;
use think\Model;


class Pay extends Model
{

    

    

    // 表名
    protected $name = 'pay';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'status_text'
    ];
    

    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function song()
    {
        return $this->belongsTo('Song', 'song_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function backPay($order_sn)
    {
        $pay = self::where('order_sn',$order_sn)->where('status',1)->find();

        if (!$pay) return false;
//        var_dump($pay['type']);
//        exit();
        $user = \model('app\common\model\User')->where('id',$pay['user_id'])->find();
        if ($pay['type'] == 2){ //终身
            $user->level = 1; //开通会员

            $recommend = \model('app\common\model\User')->where('id',$user['recommend'])->find();
            if ($user['recommend'] && $recommend['group_id'] > 1){

                if ($recommend['recommend_num'] > 0){
                    User::money($pay['pay_order'], $user['recommend'], $user['nickname']);
                    model('app\common\model\Recommend')->insert([
                    'user_id'   => $user['recommend'],
                    'number'    => '-1',
                    'last_num'  =>$user['recommend_num'],
                    'state'     =>2,
                    'mote'      =>'注册减少',
                    'to_user_id'=>$user['id'],
                    'createtime'=>time(),
                ]);

                    $recommend->setDec('recommend_num');
                }

            }
            $user->save();
        }else{
            if ($user['recommend'] && $user['group_id'] == 1){
                $data = ['user_id'=>$pay['user_id'],
                    'song_id'=>$pay['song_id'],
                    'recommend'=>$user['recommend'],
                    'createtime'=>time()
                    ];
                \model('app\common\model\buy\Song')->insert($data);
            }

            if ($user['group_id'] > 1){
                $data = ['user_id'=>$pay['user_id'],
                    'song_id'=>$pay['song_id'],
                    'recommend'=>0,
                    'createtime'=>time()
                ];
                \model('app\common\model\buy\Song')->insert($data);
            }
        }
        $pay->status = 2;

        $pay->save();
    }
}
